# studb详见studb.sql
import pymysql
class StuSys:
    #定义连接数据库
    def __init__(self):
        self.db = pymysql.connect(host="localhost", user="root", password="", database="stu",charset="utf8")
        self.cursor = self.db.cursor()


    # 查询所有
    def findAll(self):
        self.cursor.execute("select * from student")
        alist = self.cursor.fetchall()
        print("所有学生信息：")
        for i in alist:
            print(i)
    
    # 添加信息
    def insert(self,data):
        print(name,age,classid)
        sql = ("insert into student(name,age,classid) values('%s','%d','%s')"%(data))
        self.cursor.execute(sql)
        print("添加成功！")
    # 删除
    def delete(self, sid):
        self.cursor.execute("delete from student where sid={}".format(sid))
        print("删除成功！")
    #析构方法关闭数据库连接
        def __del__(self):
            self.db.close()



while 1:
    s = StuSys()
    print("=" * 12, "学员管理系统", "=" * 14)
    print("{0:1} {1:13} {2:15}".format(" ", "1. 查看学员信息", "2. 添加学员信息"))
    print("{0:1} {1:13} {2:15}".format(" ", "3. 删除学员信息", "4. 退出系统"))
    print("=" * 40)
    key = input("请输入对应的选择：")
    if key == "1":
        print("=" * 12, "学员信息浏览", "=" * 14)
        s.findAll()
        input("按回车键继续：")
    elif key == "2":
        print("=" * 12, "学员信息添加", "=" * 14)
        name = input("请输入要添加的姓名：")
        age = input("请输入要添加的年龄：")
        classid = input("请输入要添加的班级号：")
        #data = '"{}",{},"{}"'.format(name, age, classid)
        data = (name,int(age),classid)
        #print(data)
        s.insert(data)
        s.findAll()
        input("按回车键继续：")
    elif key == "3":
        print("=" * 12, "学员信息删除", "=" * 14)
        s.findAll()
        sid = input("请输入你要删除的信息sid号：")
        s.delete(sid)
        s.findAll()
        input("按回车键继续：")
    elif key == "4":
        print("=" * 12, "再见", "=" * 14)
        break
    else:
        print("======== 无效的键盘输入！ ==========")

#db.close()
